package org.snaker.engine.springboot;

import org.snaker.engine.Context;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class SprintBootContext implements Context {

    private final ApplicationContext applicationContext;

    public SprintBootContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void put(String name, Object object) {
        throw new UnsupportedOperationException("put not support");
    }

    @Override
    public void put(String name, Class<?> clazz) {
        throw new UnsupportedOperationException("put not support");
    }

    @Override
    public boolean exist(String name) {
        return applicationContext.containsBean(name);
    }

    @Override
    public <T> T find(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    @Override
    public <T> List<T> findList(Class<T> clazz) {
        return new ArrayList<>(applicationContext.getBeansOfType(clazz).values());
    }

    @Override
    public <T> T findByName(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }
}
